package com.zx.cloud.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author zhaoxuan
 * @date 2022-01-05 17:00
 **/
public class RedisMain {
    /**
     * 存储代理ipkey和代理ip的key-value对 实时刷新-map
     */
    public static final String REDISKEY_IPANDPORT = "Spider.VPS.IPAndPort";
    /**
     * //存储每个代理IP的详细信息 延迟刷新--map
     */
    public static final String REDISKEY_IPANDPORT_DETAIL = "Spider.VPS.IPAndPort.IPdetail";
    /**
     * 存储所有vnc集合 vnc白名单--set
     */
    public static final String REDISKEY_IPANDPORT_VNCSET = "Spider.VPS.IPAndPort.VNCSET";
    /**
     * 检查者队列-queue
     */
    public static final String REDISKEY_IPANDPORT_SUPERVISORS="Spider.VPS.IPAndPort.SUPERVISORS";
    /**
     * //机器资源分配-str
     */
    public static final String REDISKEY_IPANDPORT_DISTRIBUTION = "Spider.VPS.IPAndPort.DISTRIBUTION";
    /**
     * //机器资源切换IP间隔时间-str
     */
    public static final String REDISKEY_IPANDPORT_INTERVAL = "Spider.VPS.IPAndPort.INTERVAL";
    /**
     * //Whos信息-set
     */
    public static final String REDISKEY_IPANDPORT_WHOS = "Spider.VPS.IPAndPort.whos";

    public static void main(String[] args) throws IOException {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        // 最大连接数
        poolConfig.setMaxTotal(1);
        // 最大空闲数
        poolConfig.setMaxIdle(1);
        // 最大允许等待时间，如果超过这个时间还未获取到连接，则会报JedisException异常：
        // Could not get a resource from the pool
        poolConfig.setMaxWaitMillis(1000);
        Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();


        nodes.add(new HostAndPort("10.100.2.27", 6379));
        nodes.add(new HostAndPort("10.100.2.28", 6379));
        nodes.add(new HostAndPort("10.100.2.29", 6379));
        nodes.add(new HostAndPort("10.100.2.30", 6379));
        nodes.add(new HostAndPort("10.100.2.31", 6379));
        nodes.add(new HostAndPort("10.100.2.32", 6379));

        JedisCluster cluster = new JedisCluster(nodes,200000,500000,200,"fWxSvPg27fEJ",poolConfig);

        Map<String, String> rediskey_ipandport = cluster.hgetAll(REDISKEY_IPANDPORT);
        String rediskey_ipandport_distribution = cluster.get(REDISKEY_IPANDPORT_DISTRIBUTION);
        Map<String, String> rediskey_ipandport_detail = cluster.hgetAll(REDISKEY_IPANDPORT_DETAIL);
        Set<String> sdiff = cluster.smembers(REDISKEY_IPANDPORT_VNCSET);
        Set<String> whos = cluster.smembers(REDISKEY_IPANDPORT_WHOS);

        cluster.close();

        Set<HostAndPort> nodes1 = new LinkedHashSet<HostAndPort>();
        nodes1.add(new HostAndPort("10.200.1.121", 6379));
        nodes1.add(new HostAndPort("10.200.1.122", 6379));
        nodes1.add(new HostAndPort("10.200.1.123", 6379));
        nodes1.add(new HostAndPort("10.200.1.124", 6379));
        nodes1.add(new HostAndPort("10.200.1.125", 6379));
        nodes1.add(new HostAndPort("10.200.1.126", 6379));
        JedisCluster cluster1 = new JedisCluster(nodes1,200000,500000,200,"fWxSvPg27fEJ",poolConfig);
        rediskey_ipandport.forEach((k,v)->{
            cluster1.hset(REDISKEY_IPANDPORT,k,v);
        });
        cluster1.set(REDISKEY_IPANDPORT_DISTRIBUTION,rediskey_ipandport_distribution);
        rediskey_ipandport_detail.forEach((k,v)->{
            cluster1.hset(REDISKEY_IPANDPORT_DETAIL,k,v);
        });
        sdiff.forEach(x->{
            cluster1.sadd(REDISKEY_IPANDPORT_VNCSET,x);
        });
        cluster1.close();
/*        List<String> hosts= Arrays.asList("adsl7","adsl119");
        sdiff.forEach(x->{
            cluster1.sadd(REDISKEY_IPANDPORT_VNCSET,x);
        });
        hosts.forEach(host->{
            String a = rediskey_ipandport.get(host);
            //cluster1.del(REDISKEY_IPANDPORT);
            cluster1.hset(REDISKEY_IPANDPORT,host,a);
            String b = rediskey_ipandport_detail.get(host);
            //cluster1.del(REDISKEY_IPANDPORT_DETAIL);
            cluster1.hset(REDISKEY_IPANDPORT_DETAIL,host,b);
        });*/






    }
}
